package com.example.myprojectoffeixue;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

//本适配器是报警日志的适配器，主要注意通过position的不同改变调目的背景色
public class BjListadapter extends BaseAdapter {


    private ArrayList<Websocketutil> d;
    private LayoutInflater layoutInflater;
    private Context context;
    public int flag;

    public BjListadapter(Context context, ArrayList<Websocketutil> data, int flag) {
        this.context = context;
        this.flag = flag;
        this.d = data;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return d.size();
    }

    public Object getItem(int position) {
        return d.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder h = null;
        if (convertView == null) {
            // 获取组件布局
            convertView = layoutInflater.inflate(R.layout.item_baojing, null);
            h = new ViewHolder();
            h.tv = (TextView) convertView.findViewById(R.id.tv_baojingit);
            h.tv1 = (TextView) convertView.findViewById(R.id.tv1_baojingit);
            h.tv2 = (TextView) convertView.findViewById(R.id.tv2_baojingit);
            // 这里要注意，是使用的tag来存储数据的。
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        if (position % 2 != 0) {
            convertView.setBackgroundColor(Color.parseColor("#fafbfd"));
        } else {
            convertView.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        if (d.get(position).deviceName != null) {
            h.tv.setText(d.get(position).deviceName);
            if (flag == 1) {
                h.tv1.setText("被打开");
            } else {
                h.tv1.setText(d.get(position).mobile+"打开");
            }
        } else {
            if (flag == 1) {
                h.tv.setText("门磁");
                h.tv1.setText("被打开");
            } else {
                h.tv.setText("门锁");
                h.tv1.setText(d.get(position).mobile+"打开");
            }
        }

            h.tv2.setText(FormartTime(d.get(position).createTime));
        return convertView;
    }

    class ViewHolder {

        TextView tv;
        TextView tv1;
        TextView tv2;
    }


public String  FormartTime(Long time2) {
    long time = time2;
    Date date = new Date(time);
    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     return  sd.format(date);
}

}